package com.lvtongka.app.helper

import android.content.Context
import android.content.SharedPreferences
import com.lvtongka.app.base.BaseApplication
import com.lvtongka.app.ui.login.bean.LoginMember

object SPHelper {
    private const val FILE_NAME = "sp_great"
    private var sp: SharedPreferences = BaseApplication.getContext().getSharedPreferences(
        FILE_NAME,
        Context.MODE_PRIVATE
    )

    fun putLong(key: String, value: Long) {
        sp.edit().putLong(key, value).commit()
    }

    fun getLong(key: String, defValue: Long = -1) : Long {
        return sp.getLong(key, defValue)
    }

    fun putInt(key: String, value: Int) {
        sp.edit().putInt(key, value).commit()
    }

    fun getInt(key: String, defValue: Int = -1) : Int {
        return sp.getInt(key, defValue)
    }

    fun putString(key: String?, value: String?) {
        if (key == null ||value == null) {
            return
        }
        sp.edit().putString(key, value).commit()
    }

    fun getString(key: String, defValue: String = "") : String? {
        return sp.getString(key, defValue)
    }

    fun putBoolean(key: String, value: Boolean) {
        sp.edit().putBoolean(key, value).commit()
    }

    fun getBoolean(key: String, defValue: Boolean = false) : Boolean {
        return sp.getBoolean(key, defValue)
    }

    fun clearAll() {
        sp.edit().clear().commit()
        putBoolean("accept", true)
        putBoolean("accept_yd", true)
    }

    fun saveLoginInfo(login: LoginMember) {
        putBoolean(SPKConstants.LOGIN_IS_LOGIN, true)
        putString(SPKConstants.LOGIN_MEMBER_ID, login.member_id)
        putString(SPKConstants.LOGIN_ACCESS_TOKEN, login.access_token)
        putLong(SPKConstants.LOGIN_EXPIRE_TIME, login.expire_time)
    }

    fun clearLoginInfo() {
        putBoolean(SPKConstants.LOGIN_IS_LOGIN, false)
        putString(SPKConstants.LOGIN_MEMBER_ID, "")
        putString(SPKConstants.LOGIN_ACCESS_TOKEN, "")
        putLong(SPKConstants.LOGIN_EXPIRE_TIME, 0L)

        putString(SPKConstants.BASIC_INFO, "")
        putString(SPKConstants.MEMBER_ID, "")
        putString(SPKConstants.MY_CELL, "")
        putBoolean(SPKConstants.IS_CELL_BIND, false)
        putBoolean(SPKConstants.IS_REAL_NAME, false)
        putBoolean(SPKConstants.IS_SET_PWD, false)
        putBoolean(SPKConstants.IS_FUNC_SCAN, false)
        putBoolean(SPKConstants.IS_NO_PASSWORD, false)
        putBoolean(SPKConstants.PAY_MANAGER_AGREEMENT, false)
    }
}